// 第一次执行
function throttle (fn, time){
  let preTime = 0;
  function throttleFn(...args){
    const nowTime = new Date();
    if(nowTime - preTime > time) {
      fn.apply(this, args);
      preTime = nowTime;
    }
  }
  return throttleFn;
}

// 延迟后执行
function throttle (fn, time){
  let timer = null;
  function throttleFn(...args){
    const context = this;
    if(timer) return;
    timer = setTimeout(() => {
      fn.apply(context, args);
      timer = null;
    }, time)
  }
  return throttleFn;
}


// sleep函数
const sleep = async (delay) => {
  return new Promise((resole) => {
    setTimeout(resole, delay);
  });
};

// 测试
const f = throttle((count) => console.log(count), 2000);
(async function () {
  f(1); // 执行
  await sleep(1000);
  f(2); // 1000 < 2000 不执行
  await sleep(2000);
  f(3); // 1000 + 2000 > 2000 执行
  await sleep(1500);
  f(4); // 1500 < 2000 不执行
  await sleep(1000);
  f(5); // 1000 + 1500 > 2000 执行
})();